package com.dangdang.recursion;

import com.dangdang.binarytree.Node;

import java.util.Stack;

public class TraverseTest1 {


    //利用栈实现
    public static void stackTraverse(Node root){
        Stack<Node> stack = new Stack<>();
        Node node = root;
        while (node != null || !stack.isEmpty()) {
            while (node !=null) {
                stack.push(node);
                node = node.getLeft();
            }

            if (!stack.isEmpty()) {
                node =  stack.pop();
                System.out.println(node.getValue());
                node = node.getRight();
            }

        }

    }

    public static void main(String[] args) {
        Node node = new Node();
        Node.generate(node);
        TraverseTest1.stackTraverse(node);
    }
}
